package com.xlh.support.experimentshare.service.impl;

import com.xlh.aop.PreAcquireLock;
import com.xlh.support.experimentshare.dao.ExperimentShareConfigMapper;
import com.xlh.support.experimentshare.dao.helper.ExperimentShareConfigMapperHelper;
import com.xlh.support.experimentshare.domain.ExperimentShareConfig;
import com.xlh.support.experimentshare.po.ShareConfigUpdatePO;
import com.xlh.support.experimentshare.service.ExperimentShareConfigService;
import com.xlh.support.experimentshare.service.ExperimentShareService;
import com.xlh.support.experimentshare.vo.ShareConfigVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.xlh.support.experimentshare.constant.ShareConstant.DEFAULT_SHARE_CONFIG;
import static com.xlh.support.experimentshare.constant.ShareConstant.SHARE_CONFIG_KEY;

/**
 * @author cheer
 */
@Service
public class ExperimentShareConfigServiceImpl implements ExperimentShareConfigService {

    @Autowired
    private ExperimentShareService experimentShareService;

    @Autowired
    private ExperimentShareConfigMapper experimentShareConfigMapper;

    @Autowired
    private ExperimentShareConfigMapperHelper experimentShareConfigMapperHelper;

    @Override
    public ShareConfigVO getShareConfig(Long chapterId) {
        ExperimentShareConfig shareConfig = experimentShareConfigMapperHelper.getByChapterId(chapterId);
        if (shareConfig == null) {
            return ShareConfigVO.builder()
                    .stuToStuSwitch(DEFAULT_SHARE_CONFIG)
                    .stuToTeaSwitch(DEFAULT_SHARE_CONFIG)
                    .build();
        }

        return ShareConfigVO.builder()
                .stuToStuSwitch(shareConfig.getStuToStuSwitch())
                .stuToTeaSwitch(shareConfig.getStuToTeaSwitch())
                .build();
    }

    @Override
    @PreAcquireLock(value = SHARE_CONFIG_KEY)
    public void updateShareConfig(Long chapterId, ShareConfigUpdatePO shareConfigUpdatePO) {
        ExperimentShareConfig shareConfig = experimentShareConfigMapperHelper.getByChapterId(chapterId);
        if (shareConfig == null) {
            ExperimentShareConfig insertShareConfig = ExperimentShareConfig.builder()
                    .chapterId(chapterId)
                    .stuToStuSwitch(shareConfigUpdatePO.getStuToStuSwitch())
                    .stuToTeaSwitch(shareConfigUpdatePO.getStuToTeaSwitch())
                    .build();
            experimentShareConfigMapper.insertSelective(insertShareConfig);
        } else {
            ExperimentShareConfig updateShareConfig = ExperimentShareConfig.builder()
                    .id(shareConfig.getId())
                    .stuToStuSwitch(shareConfigUpdatePO.getStuToStuSwitch())
                    .stuToTeaSwitch(shareConfigUpdatePO.getStuToTeaSwitch())
                    .build();
            experimentShareConfigMapper.updateByPrimaryKeySelective(updateShareConfig);
        }

        if (!shareConfigUpdatePO.getStuToStuSwitch()) {
            experimentShareService.closeStuToStuShare(chapterId);
        }
        if (!shareConfigUpdatePO.getStuToTeaSwitch()) {
            experimentShareService.closeStuToTeaShare(chapterId);
        }
    }
}
